<?php
/**
 * Created by PhpStorm.
 * User: fyl
 * Date: 2016/11/23
 * Time: 14:14
 */

/**
 * Class Involved_ProjectModel
 */

class Involved_InvestModel extends BaseModel
{

    protected $table = 'invest';

    /**
     * 获取投资记录总数
     */
    public function GetInvestCount($item_id,$status){
        return  DB::table($this->table)
            ->select('auto_id')
            ->where('item_id',$item_id)
            ->where('status',$status)
            ->count();
    }

    /**
     * 获取投资记录列表
     */
    public function GetInvestInfo($item_id,$status){
        return  DB::table($this->table)
            ->where('item_id',$item_id)
            ->where('status',$status)
            ->get();
    }
    /**
     * 获取全部列表
     */
    public function InvestList(){
        return DB::table($this->table)
            ->select()
            ->get();
    }
    /**
     * 获取标的投资记录
     */
    public function GetInvest($paramObj){
        return  DB::table($this->table)
            ->where('item_id',$paramObj->auto_id)
            ->where('status','SUCCESS')
            ->orderBy('add_time', 'desc')
            ->get();

    }
    /**
     * 或许符合条件的红包
     */
    public function FindInvest($investWhere){
        return  DB::table($this->table)
            ->where('item_id',$investWhere['item_id'])
            ->where('status',$investWhere['status'])
            ->where('award_id','!=',0)
            ->get();
    }
    /**
     * 查询站岗利息
     */
    public function FindInvestSubsidy($investWhere){
        return  DB::table($this->table)
            ->where('item_id',$investWhere['item_id'])
            ->where('status',$investWhere['status'])
            ->get();

    }
    public function updateSubsidyAll($auto_id,$value){
        return  DB::table($this->table)
            ->where('auto_id',$auto_id)
            ->update(array('subsidy' => $value['subsidy'],'subsidy_day'=>$value['subsidy_day']));
    }

    /**
     * 用户中心投资记录
     * @param $user_id
     * @return mixed
     */
    public function UserAccountsInvestment($user_id,$page,$page_size)
    {
        if($page == 1){
            $page = $page - 1;
        }else{
            $page =$page_size*($page-1);
        }
        return  DB::table($this->table)
            ->select('item.auto_id','title','type','invest.amount','invest.add_time','invest.status','deadline','repaying_time','item.status as item_status')
            ->LeftJoin('item','item.auto_id','=','invest.item_id')
            ->where('user_id',$user_id)
            ->orderBy('invest.add_time', 'desc')
            ->skip($page)->take($page_size)
            ->get();

    }

    /**
     * 投资记录条数查询
     * @param $user_id
     * @return mixed
     */
    public function InvestCount($user_id)
    {
        return  DB::table($this->table)
            ->select('auto_id')
            ->where('user_id',$user_id)
            ->get();
    }


    /**
     * 投资记录条数条件查询
     * @param $user_id
     * @param $begin_time
     * @param $end_time
     * @return mixed
     */
    public function InvestSelCount($user_id,$begin_time,$end_time)
    {
        $begin_time = strtotime($begin_time);

        if(!empty($begin_time)&&!empty($end_time))
        {
            $end_time = strtotime($end_time)+86400;
            return  DB::table($this->table)
                ->select('auto_id')
                ->where('user_id',$user_id)
                ->whereBetween('invest.add_time',array($begin_time,$end_time))
                ->get();
        }
        if(!empty($begin_time)&&empty($end_time))
        {
            return  DB::table($this->table)
                ->select('auto_id')
                ->where('user_id',$user_id)
                ->where('invest.add_time','>',$begin_time)
                ->get();
        }

        if(!empty($end_time)&&empty($begin_time))
        {

            $end_time = strtotime($end_time)+86400;
            return  DB::table($this->table)
                ->select('auto_id')
                ->where('user_id',$user_id)
                ->where('invest.add_time','<',$end_time)
                ->get();
        }

    }

    /**
     * 投资记录条件查询
     * @param $user_id
     * @param $page
     * @param $page_size
     * @param $begin_time
     * @param $end_time
     * @return mixed
     */
    public function UserInvestSel($user_id,$page,$page_size,$begin_time,$end_time)
    {

        if($page == 1){
            $page = $page - 1;
        }else{
            $page =$page_size*($page-1);
        }
        $begin_time = strtotime($begin_time);
        if(!empty($begin_time)&&!empty($end_time))
        {

            $end_time = strtotime($end_time)+86400;
            return  DB::table($this->table)
                ->select('item.auto_id','title','type','invest.amount','invest.add_time','invest.status','deadline','repaying_time','item.status as item_status')
                ->LeftJoin('item','item.auto_id','=','invest.item_id')
                ->where('user_id',$user_id)
                ->whereBetween('invest.add_time',array($begin_time,$end_time))
                ->orderBy('invest.add_time', 'desc')
                ->skip($page)->take($page_size)
                ->get();

        }
        if(!empty($begin_time)&&empty($end_time))
        {
            return  DB::table($this->table)
                ->select('item.auto_id','title','type','invest.amount','invest.add_time','invest.status','deadline','repaying_time','item.status as item_status')
                ->LeftJoin('item','item.auto_id','=','invest.item_id')
                ->where('user_id',$user_id)
                ->where('invest.add_time','>=',$begin_time)
                ->skip($page)->take($page_size)
                ->orderBy('invest.add_time', 'desc')
                ->get();
        }

        if(!empty($end_time)&&empty($begin_time))
        {

            $end_time = strtotime($end_time)+86400;
            return  DB::table($this->table)
                ->select('item.auto_id','title','type','invest.amount','invest.add_time','invest.status','deadline','repaying_time','item.status as item_status')
                ->LeftJoin('item','item.auto_id','=','invest.item_id')
                ->where('user_id',$user_id)
                ->where('invest.add_time','<',$end_time)
                ->orderBy('invest.add_time', 'desc')
                ->skip($page)->take($page_size)
                ->get();

        }

    }

    public function SelectInvest($trade_id){
        return DB::table($this->table)
            ->where('auto_id',$trade_id)
            ->first();

    }
    public function InvestTradeNum($investWhere){
        return DB::table($this->table)
            ->where('trade_num',$investWhere['trade_num'])
            ->where('status',$investWhere['status'])
            ->first();

    }
    public function UpdateSuccess($tradeNum){
        return  DB::table($this->table)->where('trade_num',$tradeNum)->update(array('status' => 'SUCCESS'));
    }
    public function IsInvest($item_id,$user_id){
        return DB::table($this->table)
            ->where('item_id',$item_id)
            ->where('user_id',$user_id)
            ->where('status','SUCCESS')
            ->get();
    }

    /**
     * 获取邀请用户投资总额
     */
    public function UserInviteAmount($user_id,$time){
        return  DB::table($this->table)
            ->whereIn('user_id',$user_id)
            ->whereBetween('add_time',$time)
            ->sum('amount');
    }

//    /**
//     * 获取邀请用户投资总额
//     */
    public function UserInviteAmount1($user_id,$time){
        return  DB::table($this->table)
            ->select('user.user_id','user.parent_id')
            ->LeftJoin('user','user.user_id','=','invest.user_id')
            ->whereIn('invest.user_id',$user_id)
            ->whereBetween('invest.add_time',$time)
            ->groupBy('user.parent_id')
            ->sum('amount');
    }


    /**
     * 获取邀请用户投资详情
     */
    public function UserInviteInfo($user_id,$time){
        return  DB::table($this->table)
            ->select('amount','telephone','user.user_id','invest.add_time','name')
            ->LeftJoin('user','user.user_id','=','invest.user_id')
            ->whereIn('invest.user_id',$user_id)
            ->whereBetween('invest.add_time',$time)
            ->get();
    }

    /**
     * 获取邀请用户投资详情
     */
    public function GetInviteInvest($user_id,$time){
        return  DB::table($this->table)
            ->select('user_id','amount')
            ->whereIn('user_id',$user_id)
            ->whereBetween('add_time',$time)
            ->get();
    }



    //-----------------------------------------------------------------------API----------------------------------------------------------

    /**
     * 投资记录
     * @param $page
     * @param $user_id
     * @return mixed
     */
    public function InvestRecord($page,$user_id)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        return  DB::table($this->table)
            ->select('item.auto_id','title','invest.amount','invest.add_time','item.status','repaying_time','deadline','rate_loaner','user_ticket.rate','user_award.amount as award')
            ->LeftJoin('item','item.auto_id','=','invest.item_id')
            ->LeftJoin('user_ticket','user_ticket.auto_id','=','invest.ticket_id')
            ->LeftJoin('user_award','user_award.auto_id','=','invest.award_id')
            ->where('invest.user_id',$user_id)
            ->orderBy('invest.add_time', 'desc')
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->get();

    }

    /**
     * 投资红包
     */
//    public function Invest_award($user_id)
//    {
//        return  DB::table($this->table)
//            ->select('item.auto_id','title','invest.amount','invest.add_time','invest.status','repaying_time','deadline','rate_loaner','user_ticket.rate')
//            ->LeftJoin('user_award','user_award.auto_id','=','invest.award_id')
//            ->where('invest.user_id',$user_id)
//            ->orderBy('invest.add_time', 'desc')
//            ->get();
//    }


    /**
     * 资金流水
     * @param $page
     * @param $type
     * @param $user_id
     * @return mixed
     */
    public function MoneyRecord($page,$type,$user_id)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
      if(empty($type)){
          $db =   DB::table('deal_record')
              ->select('event_id','type','deal_record.user_id','deal_record.amount','deal_record.add_time','update_after')
              ->where('deal_record.user_id',$user_id)
              ->orderBy('deal_record.add_time', 'desc')
              ->skip($offset)->take(HostEnum::PAGE_NUM)
              ->get();
          foreach($db as $k=>$v){
              if($v->type=='DE_CASH'){
                  $db2 = DB::table('deal_cash')->select('status')->where('auto_id',$v->event_id)->first();
                  $v->status = $db2->status;
              }
          }
          return $db;
      }elseif($status='DE_CASH'){
          return  DB::table('deal_record')
              ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
              ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
              ->where('deal_record.user_id',$user_id)
              ->where('deal_record.type',$type)
              ->orderBy('deal_record.add_time', 'desc')
              ->skip($offset)->take(HostEnum::PAGE_NUM)
              ->get();
      }else{
          return  DB::table('deal_record')
              ->select('type','deal_record.amount','deal_record.add_time','update_after')
              ->LeftJoin('item','item.auto_id','=','invest.item_id')
              ->where('deal_record.user_id',$user_id)
              ->where('deal_record.type',$type)
              ->orderBy('invest.add_time', 'desc')
              ->skip($offset)->take(HostEnum::PAGE_NUM)
              ->get();
      }



    }
    /**
     * 获取标的投资记录
     */
    public function GetApiInvest($paramObj){
        if(isset($paramObj->page)){
            $offset = ($paramObj->page-1) * 30;
            return  DB::table($this->table)
                ->select('user_id','amount','add_time')
                ->where('item_id',$paramObj->auto_id)
                ->where('status','SUCCESS')
                ->orderBy('add_time', 'desc')
                ->skip($offset)->take(30)
                ->get();

        }else{
            return  DB::table($this->table)
                ->select('user_id','amount','add_time')
                ->where('item_id',$paramObj->auto_id)
                ->where('status','SUCCESS')
                ->orderBy('add_time', 'desc')
                ->get();
        }

    }

    /**
     * 获取邀请用户已投资人数
     */
    public function UserInvestCount($user_id,$time){
        return  DB::table($this->table)
            ->select('user_id')
            ->whereIn('user_id',$user_id)
            ->whereBetween('add_time',$time)
            ->groupBy('user_id')
            ->count();
    }





    /********************************************************M端********************************************/

    /**
     * Web投资记录
     * @param $user_id
     * @return mixed
     */
    public function WebInvestRecord($user_id,$page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        return  DB::table($this->table)
        ->select('item.auto_id','title','invest.amount','invest.add_time','invest.status','repaying_time','deadline','rate_loaner','user_ticket.rate','user_award.amount as award','item.status as item_status')
        ->LeftJoin('item','item.auto_id','=','invest.item_id')
        ->LeftJoin('user_ticket','user_ticket.auto_id','=','invest.ticket_id')
        ->LeftJoin('user_award','user_award.auto_id','=','invest.award_id')
        ->where('invest.user_id',$user_id)
        ->orderBy('invest.add_time', 'desc')
        ->skip($offset)->take(HostEnum::PAGE_NUM)
        ->get();
    }

}